package org.thss.petri.gef.commands;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.commands.Command;
import org.thss.petri.model.Place;
import org.thss.petri.model.Token;

public class CreateTokenCommand extends Command {
	private Place container;
	private Token token;
	
	public Place getContainer() {
		return container;
	}
	
	public void setContainer(EObject container) {
		if (container instanceof Place) {
			this.container = (Place) container;
		}
	}
	
	@Override
	public boolean canExecute() {
		return container != null && token != null;
	}

	@Override
	public void execute() {
		// must link container to token first; otherwise there may be problem if
		// token needs to reference its container when refreshing its visual
		token.setPlace(container);
		container.getTokens().add(token);
	}

	@Override
	public void redo() {
		execute();
	}

	@Override
	public void undo() {
		container.getTokens().remove(token);
		token.setPlace(null);
	}

	public Token getToken() {
		return token;
	}
	
	public void setToken(Token token) {
		this.token = token;
	}
}
